<?php
namespace jy_weishop_en\order;
defined('BY_JYA') or exit('error');
/*
	分销佣金
	分销插件所有金额字段均保存整形，精确到分。显示时，除以100然后保留两位小数即可
	佣金比例字段保存整形，值为万分数，相当于百分数的精确到小数点后2位。即展示到前端时，需要除以100，换算成百分数显示
*/
class refund extends \plugin{

	public $status=array(
		'0'=>'待审核',
		'1'=>'审核通过',
		'2'=>'已发货',
		'3'=>'已收货',
		'4'=>'已退款',
		'5'=>'被拒绝',
	);
	public function check_order_cancel($order=array(),$iserror=false){
		if(!$order){
			if($iserror){
				exi('订单不存在！','error');
			}else{
				return false;
			}
		}
		if($order['ispay'] && $order['status']==1){
				$refund_setting=$this->plugin_setting('refund');
				$limittime=intval($refund_setting['delmins'])*60;
				$limit=$order['paytime']+$limittime;
				if(!$refund_setting['opendel']){
					if($iserror){
						exi('本系统暂不支持付款订单取消！','error');
					}else{
						return false;
					}
				}elseif($limit<TIMESTAMP){
					if($iserror){
						exi('已超时，订单不能取消！','error');
					}else{
						return false;
					}
				}
				return true;
		}elseif($order['status']){
			if($iserror){
				exi('当前订单状态不支持取消！','error');
			}else{
				return false;
			}
		}
		return true;
	}
	/*
		检查订单是否支持退款
		订单支持退款的条件
			订单商品支持退款
			订单状态为已完成
			订单完成时间不超过七天
	*/
	public function check_order_rerefund($order_id=0,$iserror='',$goodsid=0,$ordergoodsid=0){//$iserror 报错
		$refund_setting=$this->plugin_setting('refund');
		if(!$refund_setting['open'] && !$refund_setting['openshou']){
			if($iserror){
				exi('本系统暂不支持退款！','error');
			}else{
				return false;
			}
		}
		if($order_id){
			$order=pdo_get('jy_weishop_order',array('id'=>$order_id));
			if(!$order){
				if($iserror){
					exi('订单不存在，退款失败！','error');
				}else{
					return false;
				}
			}
			if($order['status']==0 || $order['status']>3 || $order['price']<=0){
				if($iserror){
					exi('该订单状态，不支持退款！','error');
				}else{
					return false;
				}
			}
			if($order['ispay']==0){
				if($iserror){
					exi('该订单未支付，不支持退款！','error');
				}else{
					return false;
				}
			}
			$limittime=intval($refund_setting['days'])*86400;
			if($refund_setting['type']){
				$limittime=intval($refund_setting['mins'])*60;
			}
			if(!$refund_setting['statustype']){
				if($order['status']==3){
					$limit=$order['finishtime']+$limittime;
				}
			}elseif($refund_setting['statustype']==1){
				if($order['status']>=1){
					$limit=$order['paytime']+$limittime;
				}
			}elseif($refund_setting['statustype']==2){
				if($order['status']>=2){
					$limit=$order['sendtime']+$limittime;
				}
			}
			if($limit && $limit<TIMESTAMP){
				//售后
				if($refund_setting['statustype'] && $order['status']==3 && $refund_setting['openshou'] && $refund_setting['shoudays']){
					$limittime=intval($refund_setting['shoudays'])*86400;
					$limit=$order['finishtime']+$limittime;
					if($limit && $limit<TIMESTAMP){
						if($iserror){
							exi('超出退款时间，无法退款！','error');
						}else{
							return false;
						}
					}else{
						$isshouhou=1;
					}
				}else{
					if($iserror){
						exi('超出退款时间，无法退款！','error');
					}else{
						return false;
					}
				}
			}
			$params=array('orderid'=>$order_id);
			if($goodsid){
				$params['goodsid']=$goodsid;
				$refund_id=pdo_getcolumn('jy_weishop_order_refund',array('orderid'=>$order_id,'ordergoodsid'=>$ordergoodsid),'id');
				if($refund_id){
					return false;
				}
			}
			$order_goods=pdo_getall('jy_weishop_order_goods',$params,array('goodsid'),'goodsid');
			if($order_goods){
				$goods=pdo_getall('jy_weishop_goods',array('id'=>array_keys($order_goods)),array('seven'));
				foreach($goods as $item){
					if(!$item['seven']){
						if($iserror){
							exi('该商品不支持退款！','error');
						}else{
							return false;
						}
					}
				}
			}
			//非售后，未开启退款
			if(!$refund_setting['open'] && !$isshouhou){
				if($iserror){
					exi('本系统暂不支持退款！','error');
				}else{
					return false;
				}
			}
			return $isshouhou?2:1;
		}
		return false;
	}
	//拼价退款
	public function check_order_rerefund_pin($order_id=0,$iserror=''){//$iserror 报错
		if($order_id){
			$order=pdo_get('jy_weishop_order',array('id'=>$order_id));
			if(!$order){
				if($iserror){
					exi('订单不存在，退款失败！','error');
				}else{
					return false;
				}
			}
			if($order['status']!=10){
				if($iserror){
					exi('该订单状态，不支持退款！','error');
				}else{
					return false;
				}
			}
			return true;
		}
		return false;
	}
	//定价退款
	public function check_order_rerefund_ding($order_id=0,$iserror=''){//$iserror 报错
		if($order_id){
			$order=pdo_get('jy_weishop_order_ding',array('id'=>$order_id));
			if(!$order){
				if($iserror){
					exi('订单不存在，退款失败！','error');
				}else{
					return false;
				}
			}
			if($order['status']==0 || $order['status']>3){
				if($iserror){
					exi('该订单状态，不支持退款！','error');
				}else{
					return false;
				}
			}
			return true;
		}
		return false;
	}
	/*
		发起退款
	*/
	public function refund_create($params,$noexi=false){
		$order=pdo_get('jy_weishop_order',array('id'=>$params['orderid']));
		if(!$order){
			if($noexi){
				return false;
			}
			exi('指定订单不存在，发起退款失败！','error');
		}
		$isrefund=pdo_get('jy_weishop_order_refund',array('ordersn'=>$order['ordersn'],'ordergoodsid'=>intval($params['ordergoodsid']),'mid'=>$order['mid']));
		if($isrefund['id']){
			$update=array(
				'price'=>$params['price'],
				'images'=>$params['images'],
				'content'=>$params['content'],
				'status'=>0,
				'type'=>$params['type'],
				'videos'=>$params['videos'],
				'msgtypes'=>$params['msgtypes'],
				'goodsnum'=>$params['goodsnum'],
			);
			pdo_update('jy_weishop_order_refund',$update,array('id'=>$isrefund['id']));
			return $isrefund['id'];
		}
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'refundsn'=>get_ordersn('jy_weishop_order_refund','refundsn','REFUND'),
			'orderid'=>$order['id'],
			'ordersn'=>$order['ordersn'],
			'ordergoodsid'=>$params['ordergoodsid'],
			'mid'=>$order['mid'],
			'merchid'=>$order['merchid'],
			'price'=>$params['price'],
			'images'=>$params['images'],
			'content'=>$params['content'],
			'createtime'=>time(),
			'paytype'=>$order['paytype'],
			'type'=>$params['type'],
			'plugin_name'=>$params['plugin_name'],
			'videos'=>$params['videos'],
			'msgtypes'=>$params['msgtypes'],
			'goodsnum'=>$params['goodsnum'],
		);
		pdo_insert('jy_weishop_order_refund',$insert);
		$refund_id=pdo_insertid();
		$newparams=array('status <'=>5,'orderid'=>$order['id'],'uniacid'=>$_SESSION['uniacid']);
		$count=pdo_count('jy_weishop_order_goods',$newparams,'id');
		if(!$params['ordergoodsid'] || $count<=1){
			pdo_update('jy_weishop_order',array('status'=>5,'oldstatus'=>$order['status']),array('id'=>$order['id']));
		}
		$newparams=array('orderid'=>$order['id'],'uniacid'=>$_SESSION['uniacid']);
		if($params['ordergoodsid']){
			$newparams['id']=$params['ordergoodsid'];
		}
		pdo_update('jy_weishop_order_goods',array('status'=>5),$newparams);
		return $refund_id;
	}
	/*
		发起退款
	*/
	public function refund_create_ding($params){
		$order=pdo_get('jy_weishop_order_ding',array('id'=>$params['orderid']));
		if(!$order){
			exi('指定订单不存在，发起退款失败！','error');
		}
		$isrefund=pdo_get('jy_weishop_order_refund',array('orderid'=>$order['id'],'mid'=>$order['mid']));
		if($isrefund['id']){
			return false;
		}
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'refundsn'=>get_ordersn('jy_weishop_order_refund','refundsn','REFUND'),
			'orderid'=>$order['id'],
			'ordersn'=>$order['ordersn'],
			'mid'=>$order['mid'],
			'merchid'=>$order['merchid'],
			'price'=>$params['price'],
			'images'=>$params['images'],
			'content'=>$params['content'],
			'createtime'=>TIMESTAMP,
			'paytype'=>$order['paytype'],
			'plugin_name'=>$params['plugin_name'],
			'is_ding'=>1,
		);
		pdo_insert('jy_weishop_order_refund',$insert);
		$refund_id=pdo_insertid();
		pdo_update('jy_weishop_order_ding',array('status'=>5),array('id'=>$order['id']));
		return $refund_id;
	}
	/*
		审核退款
		将结果通知顾客
	*/
	public function refund_check($refund_id,$status,$reason='',$price=''){
		$update=array(
			'status'=>$status,
			'reason'=>$reason,
			'checktime'=>TIMESTAMP,
		);
		if($price){
			$update['price']=$price;
		}
		pdo_update('jy_weishop_order_refund',$update,array('id'=>$refund_id));
		return true;
	}
	/*
		发货
	*/
	public function refund_send($refund_id,$params){
		$update=array(
			'status'=>2,
			'express'=>ijson_encode($params),
			'sendtime'=>TIMESTAMP,
		);
		pdo_update('jy_weishop_order_refund',$update,array('id'=>$refund_id));
	}
	/*
		收货
	*/
	public function refund_receive($refund_id){
		$update=array(
			'status'=>3,
		);
		pdo_update('jy_weishop_order_refund',$update,array('id'=>$refund_id));
	}
	/*
		退款打款
	*/
	public function refund_pay($refund_id,$noexi=false,$isteam=false,$paytype='',$transaction_id='',$mid='',$price=''){
		//执行退款操作
		$refund=pdo_get('jy_weishop_order_refund',array('id'=>$refund_id));
		if(!$refund){
			return false;
		}
		if(($refund['status']!=1 && $refund['status']!=3 && !$isteam) || $refund['status']==4 || $refund['pay_out_id']){
			if($noexi){
				return false;
			}
			exi('退款单不符合打款要求！','error');
		}
		$refund['paytype']=$paytype?:$refund['paytype'];
		$refund['price']=$price?:$refund['price'];
		$refund['mid']=$mid?:$refund['mid'];
		$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$refund['orderid']),array('di_cardfee','ordersn','oldstatus','pay_id','transid'));
		$payinfo=pdo_get('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['pay_id']));
		if(!$transaction_id){
			$transaction_id=$order['transid']?:$payinfo['transaction_id'];
		}
		//支付方式为余额支付，退款至虚拟币
		if($refund['paytype']=='credit' || $refund['paytype']=='apipay'){
			$pay_out_id=model('core/member')->add_credit2($refund['mid'],$refund['price'],'订单退款-'.$refund['id'],$refund['plugin_name']);
		}elseif(strpos($refund['paytype'],'credit') !== false){
			$pay_out_id=model('core/member')->member_credit($refund['mid'],$refund['paytype'],$refund['price'],true,'','订单退款');
		}elseif($refund['paytype']=='wechat'){
			if(!$transaction_id){
				if($noexi){
					return false;
				}
				exi('退款失败，没有微信支付号！','error');
			}
			$payinfo['transid']=$transaction_id;
			$payinfo['money']=$refund['price'];
			$payinfo['remark']='订单退款';
			$pay_out_id=model('core/member')->refund_out($refund['mid'],$payinfo,$noexi);
		}elseif($refund['paytype']=='alipay'){
			if(!$transaction_id){
				if($noexi){
					return false;
				}
				exi('退款失败，没有支付宝支付号！','error');
			}
			$params=array(
				'money'=>$refund['price'],
				'ordersn'=>$refund['ordersn'],
				'transid'=>$transaction_id,
				'remark'=>'订单退款',
			);
			$pay_out_id=model('core/member')->refund_out($refund['mid'],$params,$noexi,'alipay');
		}elseif($refund['paytype']=='peerpay'){
			//代支付
			$peerpay_log=pdo_getall('jy_weishop_order_peerpay_log',array('status'=>1,'uniacid'=>$_SESSION['uniacid'],'orderid'=>$refund['orderid']));
			foreach($peerpay_log as $pv){
				$newpaytype=$pv['paytype'];
				$newtransid=$pv['transid'];
				$newmid=$pv['mid'];
				$newprice=$pv['money'];
				if($price>0 && $mid && $newpaytype){
					$this->refund_pay($refund_id,$noexi,$isteam,$newpaytype,$newtransid,$newmid,$newprice);
				}else{
					if($noexi){
						return false;
					}
					exi('代支付异常，无法退款到待支付客户的账户上！','error');
				}
			}
		}else{
			$params=array(
				'money'=>$refund['price'],
				'ordersn'=>$refund['ordersn'],
				'remark'=>'订单退款',
			);
			$pay_out_id=model('core/member')->pay_out($refund['mid'],$params,$noexi);
		}
		$update=array(
			'status'=>4,
			'paytime'=>TIMESTAMP,
			'pay_out_id'=>$pay_out_id,
		);
		pdo_update('jy_weishop_order_refund',$update,array('id'=>$refund_id));
		pdo_update('jy_weishop_order',array('status'=>6,'ismrefund'=>1),array('id'=>$refund['orderid']));
		pdo_update('jy_weishop_order_goods',array('status'=>6),array('orderid'=>$refund['orderid']));
		//恢复库存(待发货,拼单中)
		if($order['oldstatus']==1 || $order['oldstatus']==10){
			$getgoods=pdo_getall('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$refund['orderid']),array('goodsid','total','optionid','storeid'));
			foreach($getgoods as $item){
				if($item['optionid']){
					pdo_update('jy_weishop_goods_option',array('stock +='=>$item['total']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['optionid']));
				}else{
					pdo_update('jy_weishop_goods',array('total +='=>$item['total']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['goodsid']));
					if($item['storeid']){
						pdo_update('jy_weishop_store_goods',array('total +='=>$item['total']),array('goodsid'=>$item['goodsid'],'storeid'=>$item['storeid']));
					}
				}
			}
		}
		//退购物卡费
		if($order['di_cardfee']>0){
			$this->model('goodscard')->tuiorder($order);
		}
		return true;
	}
	/*
		订单取消免审退款
	*/
	public function order_cancelfee($order,$noexi=false){
		$this->check_order_cancel($order,true);
		$payinfo=pdo_get('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['pay_id']));
		if(!$transaction_id){
			$transaction_id=$order['transid']?:$payinfo['transaction_id'];
		}
		//支付方式为余额支付，退款至虚拟币
		if($order['paytype']=='credit' || $order['paytype']=='apipay'){
			$pay_out_id=model('core/member')->add_credit2($order['mid'],$order['price'],'订单退款-'.$order['id'],$order['plugin_name']);
		}elseif(strpos($order['paytype'],'credit') !== false){
			$pay_out_id=model('core/member')->member_credit($order['mid'],$order['paytype'],$order['price'],true,'','订单退款');
		}elseif($order['paytype']=='wechat'){
			if(!$transaction_id){
				if($noexi){
					return false;
				}
				exi('退款失败，没有微信支付号！','error');
			}
			$payinfo['transid']=$transaction_id;
			$payinfo['money']=$order['price'];
			$payinfo['remark']='订单退款';
			$pay_out_id=model('core/member')->refund_out($order['mid'],$payinfo,$noexi);
		}elseif($order['paytype']=='alipay'){
			if(!$transaction_id){
				if($noexi){
					return false;
				}
				exi('退款失败，没有支付宝支付号！','error');
			}
			$params=array(
				'money'=>$order['price'],
				'ordersn'=>$order['ordersn'],
				'transid'=>$transaction_id,
				'remark'=>'订单退款',
			);
			$pay_out_id=model('core/member')->refund_out($order['mid'],$params,$noexi,'alipay');
		}elseif($order['paytype']=='peerpay'){
			//代支付
			$peerpay_log=pdo_getall('jy_weishop_order_peerpay_log',array('status'=>1,'uniacid'=>$_SESSION['uniacid'],'orderid'=>$order['orderid']));
			foreach($peerpay_log as $pv){
				$newpaytype=$pv['paytype'];
				$newtransid=$pv['transid'];
				$newmid=$pv['mid'];
				$newprice=$pv['money'];
				if($price>0 && $mid && $newpaytype){
					$this->refund_pay($refund_id,$noexi,$isteam,$newpaytype,$newtransid,$newmid,$newprice);
				}else{
					if($noexi){
						return false;
					}
					exi('代支付异常，无法退款到待支付客户的账户上！','error');
				}
			}
		}else{
			$params=array(
				'money'=>$order['price'],
				'ordersn'=>$order['ordersn'],
				'remark'=>'订单退款',
			);
			$pay_out_id=model('core/member')->pay_out($order['mid'],$params,$noexi);
		}
		return true;
	}
}